package com.pdfbox.sample.quickstart;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;

public class _14MergePdf {

  public static void main (String args[]) throws IOException {
    String existFileName = "E:/PdfBox_Examples/sample_";
    String exportFileName = "E:/PdfBox_Examples/sample_merge.pdf";

    // 第1步:加载现有PDF文档
    File file1 = new File(existFileName + "1.pdf");
//    PDDocument doc1 = PDDocument.load(file1);
    PDDocument doc1 = Loader.loadPDF(file1);
    File file2 = new File(existFileName + "2.pdf");
//    PDDocument doc2 = PDDocument.load(file2);
    PDDocument doc2 = Loader.loadPDF(file2);

    // 第2步:实例化PDFMergerUtility类
    PDFMergerUtility PDFmerger = new PDFMergerUtility();

    // 第3步:设置目标文件
    PDFmerger.setDestinationFileName(exportFileName);

    // 第4步:设置源文件
    PDFmerger.addSource(file1);
    PDFmerger.addSource(file2);

    // 第5步:合并文档
    PDFmerger.mergeDocuments(null);
    System.out.println("Documents merged");

    // 第6步:关闭文档
    doc1.close();
    doc2.close();

    System.out.println("export " + exportFileName + " success");
  }

}
